import { defineStore } from "pinia";
import userApi, { type loginParams, type userInfoData } from "@/api/userInfo";
import { showConfirmDialog, showToast } from "vant";
import router from "@/router";

interface useUserInfoList {
    userInfo: userInfoData
}

export const useUserInfoStore = defineStore('user', {
    state(): useUserInfoList {
        return {
            // token:''
            userInfo: {
                "id": '',
                "loginName": '',
                "nickName": '',
                "passwd": '',
                "name": '',
                "phoneNum": '',
                "headImg": '',
                "userLevel": '',
                "status": 0,
                "gender": '',
                "createTime": '',
                "operateTime": '',
            }
        }

    },
    actions: {
        // 注册
        async register(loginParams: loginParams) {
            try {
                const res = await userApi.register(loginParams)
                if (res.code === 200) {
                    showToast('注册成功，请登陆账号');
                    router.push('/login')
                }
            } catch (error) {
                return Promise.reject(error)
            }
        },

        // // 用户登录
        // async login(loginParams:loginParams){
        //     try {
        //         const res = await userApi.Login(loginParams)
        //         this.token = res.data.token
        //         return 'ok'
        //     } catch (error) {
        //         return Promise.reject(error)
        //     }

        // },

        // 获取验证码
        async getCode(phone: string) {
            const res = await userApi.getCode(phone)
        },

        // 获取用户信息
        async getUserInfo() {
            const res = await userApi.getUserInfo()
            if (res.code === 200) {
                this.userInfo = res.data
            } else {
                router.push('/login')
            }
        },

        // 退出登录
        async logout() {

            showConfirmDialog({
                title: '标题',
                message:
                    '三十年河东,三十年河西,莫欺网页穷。',
            })
                .then(async () => {
                    // on confirm
                    await userApi.Logout()
                    localStorage.removeItem('token_key')
                    router.push('/')
                })
                .catch(() => {
                    // on cancel
                });
        }
    },
    getters: {

    }
})